<template>
<div class='roles'>
    <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>权限管理</el-breadcrumb-item>
        <el-breadcrumb-item>角色列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片 -->
    <el-card>
        <!-- 添加角色按钮 -->
        <el-row>
            <el-col>
                <el-button type="primary">添加角色</el-button>
            </el-col>
        </el-row>
        <!-- 角色列表 -->
        <el-table :data="roleList" border stripe>
            <!-- 展开列表 -->
            <el-table-column type="expand">
                <template slot-scope="scope" >
                    <el-row v-for="(item,i1) in scope.row.children" :key="item.id" :class="['bottom', i1 === 0 ? 'top' : ' ','vercenter']">
                        <!-- 渲染一级权限 -->
                        <el-col :span="5">
                            <el-tag :closable="true">{{item.autoName}}</el-tag><i class="el-icon-caret-right"></i>
                        </el-col>
                        <!-- 渲染二级\三级权限 -->
                        <el-col :span="19">
                            <!-- 渲染二级权限 -->
                            <el-row v-for="(item2,i2) in item.children" :key="item2.id" :class="['bottom', i2 === 0 ? 'top' : ' ','vercenter']">
                                <el-col :span="6">
                                    <el-tag :closable="true" type="success">{{item2.autoName}}</el-tag><i class="el-icon-caret-right"></i>
                                </el-col>
                                <el-col :span="18">
                                    <el-tag @close="handleClose" closable type="warning" v-for="(item3) in item2.children" :key="item3.id">{{item3.autoName}}</el-tag>
                                </el-col>
                            </el-row>
                        </el-col>
                    </el-row>
                </template>
            </el-table-column>
            <!-- 索引列 -->
            <el-table-column type="index" label="序号"></el-table-column>
            <el-table-column label="角色名称" prop="autoName"></el-table-column>
            <el-table-column label="角色描述" prop="roleDesc"></el-table-column>
            <el-table-column label="操作" prop="leave">
                <template slot-scope="scope" >
                    <el-button size="mini" type="primary" icon="el-icon-edit">编辑</el-button>
                    <el-button size="mini" type="danger" icon="el-icon-detele">删除</el-button>
                    <el-button size="mini" type="warning" icon="el-icon-setting" @click="showSetRightDialog(scope.row)">分配</el-button>
                </template>
            </el-table-column>
        </el-table>
    </el-card>
    <!--分配权限对话框-->
    <el-dialog
        title="分配权限"
        :visible.sync="SetRightsVisible"
        width="50%"
        :before-close="handleRightsClose">
        <!-- 树形控件 -->
        <el-tree :data="roleList" ref="treeRef" :props="threeProps" showCheckbox node-key="id" :default-checked-keys='defaultKeys'></el-tree>
        <span slot="footer" class="dialog-footer">
            <el-button @click="SetRightsVisible = false">取 消</el-button>
            <el-button type="primary" @click="allotRights">确 定</el-button>
        </span>
    </el-dialog>
</div>
</template>
<script>
export default {
name: 'Roles',
data(){
    return {
        roleList:[
            {
                id:1,
                autoName:"主管",
                roleDesc:'主管好厉害',
                children:[
                   {
                        id:2,
                        autoName:'商品管理',
                        path:null,
                        children:[
                           {
                               id:3,
                               autoName:'添加商品',
                               path:null,
                           },
                           {
                               id:7,
                               autoName:'添加商品',
                               path:null
                           },
                           {
                               id:8,
                               autoName:'添加商品',
                               path:null
                           },
                           {
                               id:9,
                               autoName:'添加商品',
                               path:null
                           }
                       ]
                   },
                   {
                        id:6,
                        autoName:'列表管理',
                        path:null,
                        children:[
                           {
                               id:3,
                               autoName:'添加商品',
                               path:null
                           },
                           {
                               id:10,
                               autoName:'添加商品',
                               path:null
                           },
                           {
                               id:11,
                               autoName:'添加商品',
                               path:null
                           }
                       ]
                   },
                   {
                        id:5,
                        autoName:'人员管理',
                        path:null,
                        children:[
                           {
                               id:3,
                               autoName:'添加商品',
                               path:null
                           },
                           {
                               id:12,
                               autoName:'添加商品',
                               path:null
                           },
                           {
                               id:13,
                               autoName:'添加商品',
                               path:null
                           }
                       ]
                   },
                   {
                        id:4,
                        autoName:'产品管理',
                        path:null,
                        children:[
                           {
                               id:3,
                               autoName:'添加商品',
                               path:null
                           },
                           {
                               id:81,
                               autoName:'添加商品',
                               path:null
                           },
                           {
                               id:29,
                               autoName:'添加商品',
                               path:null
                           }
                       ]
                   }
               ]
            },
        ],
        SetRightsVisible: false,
        threeProps:{
            label: 'autoName',
            children: 'children',
        },
        defaultKeys:[29]
    }
},
created(){
    
},
computed: {
    rolelistdata:function(){
        return this.roleList.children
    }
},
methods:{
    // 获取角色信息
    getRolesList(){
        // this.$http.get()
    },
    async handleClose() {
       const confirmResult =  await this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).catch(err => err)

        if(confirmResult !== 'confirm'){
            return this.$message('取消删除')
        }
        // 确认删除
        
    },
    // 展示分配权限的对话框
    showSetRightDialog(role){
        // 递归获取当前三级节点

        // this.getLeafKeys(role, this.defaultKeys)

        this.SetRightsVisible = true
    },
    async handleRightsClose(){
        const confirmResult =  await this.$confirm('关闭后权限设置将无法保存, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).catch(err => err)
        if(confirmResult !== 'confirm'){
            return this.$message('取消删除')
        }

        this.SetRightsVisible = false
        // 确认删除
    },
    // 通过递归的形式，获取角色下所有的三级权限的id，并保存到 defKeys 数组中
    getLeafKeys(node, arr) {
        // 如果当前node 节点不包含chidren属性，则是三级节点
        if(!node.chiildren){
            return arr.push(node.id)
        }
        node.children.forEach(item => {
            this.getLeafKeys(item,arr)
        });

    },
    // 点击为角色分配权限
    allotRights(){
        const keys = [
            ...this.$refs.treeRef.getCheckedKeys(),
            ...this.$refs.treeRef.getHalfCheckedKeys()
        ]
        console.log(keys)
        this.SetRightsVisible = false
    }
}
}
</script>
<style lang='less' scoped>
.el-table{
    margin-top: 15px;
}
.top{
    border-top:1px solid #e2e2e2;
}
.bottom{
    border-bottom: 1px solid #e2e2e2;
    padding:15px 0;
}
.el-table-column{
    padding:15px!important;
}
.vercenter{
    display:flex;
    align-items: center;
}
</style>